import Mock from 'mockjs'

/*Mock.setup({
  timeout:'500-3000'
});*/

const random = Mock.Random

function randomColor () {
  return '#' + Math.floor(Math.random() * (1000000 - 99999) + 99999)
}

const homeData = () => {
  let data = [];
  for (var i = 0; i < 30; i++) {
    data.push({
      pid: random.guid(),
      pimg: random.image('300X300', randomColor(),'#ffffff', 'prod' + i),
      psupTitle: random.csentence(10, 20),
      psubTitle: random.csentence(8, 12),
      pselled: random.integer(10, 9999),
      pcurrentPrice: random.float(20, 300, 1, 9),
      pdiscount: random.float(0, 1),
      quantity:0,
      check:false
    })
  }
  return data
};

const commodityDetails = () => {
  let cotyImageTextImg = [];
  let cotyImgText = [];
  let cotyImgArray = [];
  for(let i = 0; i < Math.ceil(random.float(3, 6)); i++){
    cotyImageTextImg.push(
      random.image('300X300', randomColor(),'#ffffff', 'prod' + i),
    );
    cotyImgText.push(
      {
      img:random.image('300X300', randomColor(),'#ffffff', 'prod' + i),
        text:random.csentence(6, 20),
        price:random.float(20, 300, 1, 9),
      },
    );
    cotyImgArray.push(
      random.image('300X300', randomColor(),'#ffffff', 'prod' + i),
    )
  }
  let data = [];
  for(let i = 1; i < 2; i++){
    data.push({
      pid:'@id',
      pimg: random.image('300X300', randomColor(),'#ffffff', 'prod'),
      psubTitle:random.csentence(6, 12),
      psupTitle:random.csentence(12, 20),
      cotyCondition:['29免邮','1小时达','30秒退款'],
      cotyImageText:cotyImgText,
      pcurrentPrice:random.float(20, 300, 1, 9),
      cotyParameter:random.csentence(12, 20),
      cotyImg:cotyImgArray,
      pdiscount:random.float(0, 1),
      cotyEvaluate:random.float(1, 5),
      cotyRemark:random.csentence(6, 20),
      quantity:0,
    });
  }
  return data;
};
// 原有地址
Mock.mock('/location','get',()=>{
  return Mock.mock({
    'data|3-4':[
      {
        locId:'@id',
        locUser:'@cname',
        locTel:/^1[35789][0-9]{9}$/,
        locCity:'@county(true)',
        locSite:'@csentence(1,5)'
      }
    ]
  })
});
// 新增地址
Mock.mock('/add/location','post',(req)=>{
  if((!(JSON.parse(req.body).username))
  &&(!(JSON.parse(req.body).userTel))
    &&(!(JSON.parse(req.body).location))
    &&(!(JSON.parse(req.body).detailedAddress))
  ){
    return Mock.mock({
      data:[
        {
          code:-1,
          msg:'请输入合法信息'
        }
      ]
    });
  }else {
    return Mock.mock({
      data:[
        {
          code:1,
        }
      ]
    });
  }
});
// 删除地址
Mock.mock('/del/location', 'post',req=>{
  if(JSON.parse(req.body).id){
    return Mock.mock({
      data:[
        {
          code:1,
          msg:'删除成功'
        }
      ]
    });
  }
});
// 加入购物车
Mock.mock('/add/shop', 'post',req=>{
  if(JSON.parse(req.body).id){
    return Mock.mock({
      data:[
        {
          code:1,
          msg:'加入购物车成功'
        }
      ]
    });
  }
});
//购物车商品
Mock.mock('/shopping','get', ()=>{
  return Mock.mock({
    'data|2-4':[
      {
        'pid':'@id',
        'psubTitle':'@csentence(12, 20)',
        'pcurrentPrice':'@integer(60, 100)',
        'pimg':random.image('100x100', randomColor(), '#ffffff', '@name'),
        'quantity':'@increment(1,7)',
        'check':false
      }
    ]
  })
});
// 减商品数量
Mock.mock('/del/cart/number','post', req=>{
  if((JSON.parse(req.body).pid)&&JSON.parse(req.body).num){
    return Mock.mock({
      data:[
        {
          code:1,
          msg:'商品数量-1'
        }
      ]
    });
  }else if(JSON.parse(req.body).num === 0){
    return Mock.mock({
      data:[
        {
          code:-1,
          msg:'商品数量为0'
        }
      ]
    });
  }
});
// 加商品数量
Mock.mock('/add/cart/number','post', req=>{
  if((JSON.parse(req.body).pid)&&JSON.parse(req.body).num){
    return Mock.mock({
      data:[
        {
          code:1,
          msg:'商品数量+1'
        }
      ]
    });
  }
});

Mock.mock('http://miao.cn', 'get', homeData());
Mock.mock('http://miao.commodity.cn', 'get', commodityDetails());


